<template>
  <div>
    <el-form >
      <el-form-item label="名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="form.sort"></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button @click="$emit('cancel')">取消</el-button>
        <el-button type="primary" @click="onSubmit">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { saveOrderUpdate } from '@/services/resource-category'
export default Vue.extend({
  name: 'CategoryCreateOrEdit',
  props: {
    categoryRecord: {
      type: Object
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        name: '',
        sort: 0
      }
    }
  },

  created () {
    if (this.isEdit) {
      this.form = { ...this.categoryRecord }
    }
  },

  methods: {
    async onSubmit () {
      // 1. 表单验证
      // 2. 验证通过，提交表单
      const { data } = await saveOrderUpdate(this.form)
      if (data.code === '000000') {
        this.$message.success('操作成功')
        this.$emit('success')
      }
    }
  }
})
</script>

<style lang="scss" scoped></style>
